package com.wf.day_0608.test3;

import java.util.Random;

/**
 * @author mpk
 * @since 2024/6/8 20:57:53
 */
public class RedPack extends Thread {

    public RedPack(String name) {
        super(name);
    }

    static double red = 100;
    Random random = new Random();

    static int count = 1;

    @Override
    public void run() {


            synchronized (RedPack.class) {
                if (red == 0) {
                    System.out.println(Thread.currentThread().getName() + "没抢到");
                } else {
                    if (count <= 2) {
                        double i = random.nextInt(100);
                        while (red - i < 0) {
                            i = random.nextInt(100);
                        }
                        System.out.println(Thread.currentThread().getName() + "抢到了" + i + "元");
                        red = red-i;
                        count++;
                    } else if (count == 3) {
                        System.out.println(Thread.currentThread().getName() + "抢到了" + red + "元");
                        count++;
                        red=0;
                    }

                }

            }



    }
}
